package com.maixy.studyonlineapi.controller;

import com.maixy.studyonlineapi.apis.ApiController;
import com.maixy.studyonlineapi.apis.ApiRequest;
import com.maixy.studyonlineapi.apis.ApiResponse;
import com.maixy.studyonlineapi.apis.Privilege;
import com.maixy.studyonlineapi.bean.Page;
import com.maixy.studyonlineapi.exceptions.MissParamException;
import com.maixy.studyonlineapi.exceptions.TipException;
import com.maixy.studyonlineapi.result.PageData;
import com.maixy.studyonlineapi.service.ActivityContentService;
import com.maixy.studyonlineapi.utils.DataTypeUtil;
import com.maixy.studyonlineapi.view.ActivityContentView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName ActivityContentController
 * @Description TODO
 * @Date 2020/11/29 17:22
 * @Author maixy
 */
@RestController
@RequestMapping("/v1/activity/content")
public class ActivityContentController extends ApiController {
    @Resource
    private ActivityContentService activityContentService;

    /**
     * 获得课程内容列表
     * @param request -- activityId 活动id
     *                -- activityContentSearch 搜索关键字
     * @return --
     */
    @Privilege
    public ApiResponse activityContentList(ApiRequest request) {
        Long activityId = request.getDataParamAsLong("activityId");
        String activityContentSearch = request.getDataParamAsString("activityContentSearch", "");
        Boolean hasStudent = request.getDataParamAsBool("hasStudent", false);
        Page page = Page.validation(request.getPage());
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());

        if (null == activityId) {
            throw new MissParamException();
        }

        PageData<ActivityContentView> pageData = activityContentService.activityContentList(activityId, userId, activityContentSearch, hasStudent, page);
        return ApiResponse.ok()
                .addValueToData("pageData", pageData);
    }

    /**
     * 添加课程内容
     * @param request -- activityId 活动id
     *                -- activityContentUrl 媒体文件Url
     *                -- contentLevel 内容登记
     *                -- contentName 内容名称
     * @return --
     */
    @Privilege
    public ApiResponse saveActivityContent(ApiRequest request) {
        Long activityId = request.getDataParamAsLong("activityId");
        String activityContentUrl = request.getDataParamAsString("activityContentUrl");
        Integer contentLevel = request.getDataParamAsInt("contentLevel");
        Integer contentType = request.getDataParamAsInt("contentType");
        String contentName = request.getDataParamAsString("contentName");
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long teamId = DataTypeUtil.getLong(request.getTeamId());

        if (null == activityId || StringUtils.isBlank(activityContentUrl) || StringUtils.isBlank(contentName) || null == contentLevel || null == contentType) {
            throw new TipException("参数缺失");
        }

        if (0 >= contentLevel || 3 < contentLevel) {
            throw new TipException("暂无此等级的课件");
        }

        if (1 >= contentType || 3 < contentType) {
            throw new TipException("暂无此类型的课件");
        }
        activityContentService.saveActivityContent(teamId, userId, activityId, activityContentUrl, contentLevel, contentType, contentName);
        return ApiResponse.ok();
    }

    /**
     * 删除课程内容
     * @param request -- activityContentId 课程内容id
     * @return --
     */
    @Privilege
    public ApiResponse deleteActivityContent(ApiRequest request) {
        Long activityContentId = request.getDataParamAsLong("activityContentId");
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long teamId = DataTypeUtil.getLong(request.getTeamId());

        if (null == activityContentId) {
            throw new TipException("参数缺失");
        }
        activityContentService.deleteActivityContent(teamId, userId, activityContentId);
        return ApiResponse.ok();
    }
}
